package main

import (
	"context"
	"fmt"
	"gosync/config"
	"gosync/message"
	bsSync "gosync/sync"
	"log/slog"
	"net/http"
	"runtime"
	"sync"
	"time"
)

var operationApiUrl = ""
var bsApiUrl = ""
var syncRunFlag = false

// operationApiUrl = "http://10.78.139.1:8062"
// bsApiUrl = "http://128.128.10.18"

// var operationApiUrl = "http://dev.bs.com"
// var bsApiUrl = "http://dev.bs.com"

func main() {
	fmt.Println("当前GOMAXPROCS:", runtime.GOMAXPROCS(0))
	// var GOMAXPROCS int
	// GOMAXPROCS = runtime.GOMAXPROCS(0)
	wg := sync.WaitGroup{}
	fmt.Println("开始执行:", time.Now().Format("2006-01-02 15:04:05"))
	for i := 0; i < 16; i++ {
		wg.Add(1)
		go func(v int) {
			fmt.Println("开始:", v, time.Now().Format("2006-01-02 15:04:05"))
			time.Sleep(3 * time.Second)
			//b, _ := io.ReadAll(resp.Body)
			//fmt.Println("结束:", v, b)
			fmt.Println("结束:", v, time.Now().Format("2006-01-02 15:04:05"))

			wg.Done()
		}(i)
	}
	wg.Wait()
	fmt.Println("执行完成:", time.Now().Format("2006-01-02 15:04:05"))

	return
	operationApiUrl = config.SyncConfig.OperateHost
	bsApiUrl = config.SyncConfig.BsHost
	fmt.Println(operationApiUrl, bsApiUrl)

	go func() {

		http.HandleFunc("/sync_datas", sync_data)
		http.ListenAndServe(":9090", nil)
	}()

	ticker := time.NewTicker(60 * time.Second)
	defer ticker.Stop() // 确保在不再需要时停止Ticker

	//使用select语句持续等待Ticker信号
	for {
		select {
		case <-ticker.C:
			uiq := time.Now().Format("2006-01-02 15:04:05")
			// 注意：这里的t是每次Ticker发送的时间值，但在这个例子中我们不需要它
			messageId := bsSync.GenerateRandomNumberString(10)
			//ctx := context.WithValue(context.Background(), "crontab_request_id", messageId)
			ctx := message.AppendCtx(context.Background(), slog.String("request_id", messageId))
			if syncRunFlag {
				message.LogMsg.InfoContext(ctx, "9090端口服务在同步中")
				break
			}
			syncRunFlag = true

			message.LogMsg.InfoContext(ctx, "=====>同步任务开始执行")

			wg := sync.WaitGroup{}
			wg.Add(2)
			go func() {
				defer bsSync.CatchException()
				defer wg.Done()
				message.LogMsg.InfoContext(ctx, "开始执行同步方法"+"syncDataToBs")
				syncDataToBs(ctx)
				fmt.Println(uiq, "syncDataToBs Task executed start  at", time.Now())
				message.LogMsg.InfoContext(ctx, "结束执行同步方法"+"syncDataToBs")
			}()

			go func() {
				defer bsSync.CatchException()
				defer wg.Done()
				message.LogMsg.InfoContext(ctx, "开始执行同步方法"+"syncDataToOperation")
				syncDataToOperation(ctx)
				fmt.Println(uiq, "syncDataToOperation Task executed start  at", time.Now())
				message.LogMsg.InfoContext(ctx, "结束执行同步方法"+"syncDataToOperation")
			}()
			wg.Wait()

			syncRunFlag = false

			message.LogMsg.InfoContext(ctx, "=====>同步任务结束执行")

		}
	}

	//同步配置
	// syncConfigHost := bsSync.SyncHostConfig{
	// 	GetDataHost:           operationApiUrl,
	// 	ReceiveDataHost:       bsApiUrl,
	// 	SelfServiceTimeStamp:  0,
	// 	ThirdServiceTimeStamp: 0,
	// }
	// syncConfigHost.SyncFile()
}

func sync_data(w http.ResponseWriter, r *http.Request) {
	if syncRunFlag {
		w.Write([]byte("定时任务正在同步中！"))
		return
	}
	wg := sync.WaitGroup{}
	uiq := time.Now().Format("2006-01-02 15:04:05")
	ctx := context.Background()
	wg.Add(2)
	go func() {
		defer wg.Done()
		syncDataToBs(ctx)
		fmt.Println(uiq, "syncDataToBs Task executed start  at", time.Now())
	}()

	go func() {
		defer wg.Done()
		syncDataToOperation(ctx)
		fmt.Println(uiq, "syncDataToOperation Task executed start  at", time.Now())
	}()
	wg.Wait()

	w.Write([]byte("同步成功！"))
}

func syncDataToBs(ctx context.Context) {
	//同步配置
	ctx = message.AppendCtx(ctx, slog.String("to_where", "BS"))
	syncConfigHost := bsSync.SyncHostConfig{
		GetDataHost:           operationApiUrl,
		ReceiveDataHost:       bsApiUrl,
		SelfServiceTimeStamp:  0,
		ThirdServiceTimeStamp: 0,
		Ctx:                   ctx,
	}

	syncConfigHost.SyncDataFlow()
	syncConfigHost.SyncFile()
}

func syncDataToOperation(ctx context.Context) {
	//同步配置
	ctx = message.AppendCtx(ctx, slog.String("to_where", "OP"))
	syncConfigHost := bsSync.SyncHostConfig{
		GetDataHost:           bsApiUrl,
		ReceiveDataHost:       operationApiUrl,
		SelfServiceTimeStamp:  0,
		ThirdServiceTimeStamp: 0,
		Ctx:                   ctx,
	}
	syncConfigHost.SyncDataFlow()
	syncConfigHost.SyncFile()
}

func generateTimeBasedID() string {

	return fmt.Sprintf("%d", time.Now().UnixNano())
}
